package com.scap.authen.dao;


import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.scap.authen.entity.Application;

@Service
@Repository("voApplicationDao")
public class ApplicationDaoImpl extends AbstractDao implements ApplicationDao{



	@SuppressWarnings("unchecked")
	@Override
	public List<Object> getAllApplication() {

		Criteria criteria = getSession().createCriteria(Application.class);
		List<Object> voApplicationsList = (List<Object>)criteria.list();
		shutdownSession();
		return voApplicationsList;
	}


	@Override
	public boolean insertApplication(Application voApplication) {
		boolean result = false;
		
		try {
			persist(voApplication);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		
		return result;
	}
	

	@Override
	public boolean updateApplication(Application voApplication) {
		boolean result = false;
		
		try {
			
			Query query = getSession().createQuery("update Application set vaApplicationName = :vaApplicationName ,"
					+ "vaImageUrl = :vaImageUrl ,"
					+ "vaLink = :vaLink"
    				+ " where vaApplicationId = :vaApplicationId");
			
			query.setString("vaApplicationName", voApplication.getVaApplicationName());
			query.setString("vaImageUrl", voApplication.getVaImageUrl());
			query.setString("vaLink", voApplication.getVaLink());
			query.setString("vaApplicationId", voApplication.getVaApplicationId());
			query.executeUpdate();
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		
		return result;
	}

	@Override
	public boolean deleteApplication(Application voApplication) {
		boolean result = false;
		
		try {
			delete(voApplication);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		
		return result;
	}
	
}
